package org.quickserver.net.GB268753QN;


import com.dykj.fireFighting.web.model.report.DevHisSs;
import org.quickserver.net.model.ShengShanServerData;
import org.quickserver.net.server.ClientHandler;
import org.quickserver.util.data.Crc16;
import org.quickserver.util.data.dataProcessing;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 盛善设备数据接收
 */
public class analysisMainSS
{
  public static DevHisSs init(ClientHandler handler, String[] strs) throws IOException {
    String logStrAll = dataProcessing.strsToStr(strs);
    ShengShanServerData ec = (ShengShanServerData)handler.getClientData();
    DevHisSs zcModel = new DevHisSs();
    int strsLength = strs.length;
    int otherCommand = dataProcessing.hexStringToInt10(strs[0]);
    String crcData = strs[strsLength-2]+strs[strsLength-1];//校验

    //data
    String[] mainMata = dataProcessing.strsToStrs(strs, 3, strsLength - 3);

    byte[] bytesCrc = dataProcessing.hexStringsToByteArray(dataProcessing.strsToStrs(strs, 3, strs.length - 3));
    String crc = dataProcessing.addZeroForNum(Crc16.getCRC2(bytesCrc), 4);
    String[] crcs = dataProcessing.strToStrs(crc);
    crc = crcs[1] + crcs[0];
    crc=crc.toUpperCase();

    System.out.println("盛善命令-------" + strs[0]);
    if ((!ec.getUniqueCode().equals("0") || otherCommand==253) && crc.equals(crcData)) {
      try {
        Object o;
        Class<?> clazz =
          Class.forName("org.quickserver.net.Shengshan.control.SS" + otherCommand);
        Method method = clazz.getMethod("Shengshan" + otherCommand + "OBJ", new Class[] { ClientHandler.class,
              String[].class });
        o = method.invoke(clazz.newInstance(), new Object[] { handler, mainMata });
        if (o == null) {
          zcModel = null;
        }
        else {
          zcModel = (DevHisSs) o;
        }

      } catch (IllegalAccessException e) {


        zcModel = null;
        return zcModel;
      } catch (IllegalArgumentException e) {


        zcModel = null;
        return zcModel;
      } catch (InvocationTargetException e) {


        zcModel = null;
        return zcModel;
      } catch (InstantiationException e) {


        zcModel = null;
        return zcModel;
      } catch (NoSuchMethodException e1) {


        zcModel = null;
        return zcModel;
      } catch (SecurityException e1) {


        zcModel = null;
        return zcModel;
      } catch (ClassNotFoundException e2) {
        zcModel = null;
        return zcModel;

      }
    } else {
    }
    return zcModel;
  }
}
